﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;

namespace BusinessLayer
{
    /// <summary>
    /// The car state.
    /// </summary>
    public class CarState : BusinessEntity
    {
        /// <summary>
        /// The car state repository.
        /// </summary>
        public class CarStateRepository : Repository<CarState>
        {
            /// <summary>
            /// Remove States of the car before time specified.
            /// </summary>
            public void Remove(int carId, DateTime time)
            {
                DAL.Operation("Remove", "CarState", new Dictionary<string, object> { { "CarId", carId }, { "Time", time } });
            }

            /// <summary>
            /// Remove all states of the car.
            /// </summary>
            public override void Remove(int id)
            {
                DAL.Operation("Remove", "CarState", new Dictionary<string, object> { { "CarId", id } });
            }

            /// <summary>
            /// Remove states of all cars before time specified.
            /// </summary>
            public void Remove(DateTime time)
            {
                DAL.Operation("Remove", "CarState", new Dictionary<string, object> { { "Time", time } });
            }
        }

        /// <summary>
        /// The car state repository.
        /// </summary>
        public static CarStateRepository Repository = new CarStateRepository(); 

        /// <summary>
        /// Gets the car id.
        /// </summary>
        public int CarId
        {
            get { return (int)this.Get("CarId"); }
            set { this.Set("CarId", value); }
        }

        /// <summary>
        /// Gets the location.
        /// </summary>
        public Point Location
        {
            get { return new Point((double)this.Get("LocationX"), (double)this.Get("LocationY")); }
            set { this.Set("LocationX", value.X); this.Set("LocationY", value.Y); }
        }

        /// <summary>
        /// Gets the speed.
        /// </summary>
        public double Speed
        {
            get { return (double)this.Get("Speed"); }
            set { this.Set("Speed", value); }
        }

        /// <summary>
        /// Gets the oil pressure.
        /// </summary>
        public double OilPressure
        {
            get { return (double)this.Get("OilPressure"); }
            set { this.Set("OilPressure", value); }
        }

        /// <summary>
        /// Gets the gas pressure.
        /// </summary>
        public double GasPressure
        {
            get { return (double)this.Get("GasPressure"); }
            set { this.Set("GasPressure", value); }
        }

        /// <summary>
        /// Gets the time.
        /// </summary>
        public DateTime Time
        {
            get { return (DateTime)this.Get("Time"); }
            set { this.Set("Time", value); }
        }

        public double Direction
        {
            get { return (double)this.Get("Direction"); }
            set { this.Set("Direction", value); }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CarState"/> class.
        /// </summary>
        internal CarState(Dictionary<string, object> carState)
        {
            this.Initialize(carState);
        }

        /// <summary>
        /// Default constructor.
        /// Must call Initialize method before using.
        /// </summary>
        public CarState()
        {
        }

        /// <summary>
        /// Save the car state.
        /// </summary>
        public override void Save()
        {
            CarState.Repository.Write(this);
        }
    }
}
